/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mscs.server.logic;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author Usbuild
 */
public class NetUtil {
    public ServerSocket serverSock;
    public final String tempDir = new SettingUtil().get("shopsPath");
    public void start() {
        new Thread(new Server()).start();
    }
    class Server extends Thread {
        @Override
        public void run() {
            try {
                serverSock = new ServerSocket();
                serverSock.bind(new InetSocketAddress(InetAddress.getLocalHost(), 5001));
                while(true) {
                    Socket tmpSock = serverSock.accept();
                    new Thread(new newSocket(tmpSock)).start();
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    class newSocket extends Thread {
        String spliter = "------";
        private Socket tmpSock;
        public newSocket(Socket sock) {
            this.tmpSock = sock;
        }
        @Override
        public void run() {
            try {
                InputStream is = tmpSock.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String fileName = br.readLine();
                if(fileName == null) return;
                OutputStream os = tmpSock.getOutputStream();
                String folerName = fileName.split(spliter)[0];
                String folderPath = tempDir + File.separator + folerName;
                fileName = fileName.substring(folerName.length() + spliter.length());
                
                if(fileName.charAt(0) != '*') {
                    String filePath = folderPath + File.separator + fileName;
                    File folder = new File(folderPath);
                    if(!folder.exists())
                        folder.mkdirs();
                    File file = new File(filePath);
                    file.createNewFile();
                    FileOutputStream fos = new FileOutputStream(file);
                    byte bt1 ,bt2;
                    int size;
                    byte[] buff = new byte[2048];
                    while((bt1 = (byte)is.read()) != -1) {
                        bt2 = (byte)is.read();
                        size = bt1 * 127 + bt2;
                        if(size == 2048) {
                            is.read(buff);
                            fos.write(buff);
                        }
                        else {
                            byte[] buf = new byte[size];
                            is.read(buf);
                            fos.write(buf);
                            break;
                        }
                    }
                    fos.close();
                } else {
                    //Send files to clients
                    fileName = fileName.substring(1);
                    File file = new File(folderPath + File.separator + fileName);
                    Socket sock = new Socket();
                    sock.connect(new InetSocketAddress(InetAddress.getLocalHost(), 5001));
                    FileInputStream fis = new FileInputStream(file);
                    Thread.sleep(10);
                       int size;
                       int bt1, bt2;
                       byte[] buff = new byte[2048];
                       while((size = fis.read(buff)) != -1) {
                           bt1 = size / 127;
                           bt2 = size % 127;
                           os.write(bt1);
                           os.write(bt2);
                           os.write(buff);
                       }
                       
                    fis.close();
                    os.close();
                    sock.close();
                }
                is.close();
                os.close();
                br.close();
                tmpSock.close();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
}
